/* Author: 
 Aureliano
 */

/* FULL BANNER MID-LARGE */

$('.full-img img').on('load', function() {
    $('#top-banner .loader').fadeOut(300);
    $('.full-img').fadeIn(300);
    center();
    fullBg();
});

$(window).on('resize', function() {
    center();
    $('#top-banner').height($(window).height());
    fullBg();
    if($(window).width() > 790 && menuState == 1){
        $('.menu').trigger('click');
    }
});

function fullBg() {
    var background = $('.full-img');
    var h = $(window).height();
    background.height(h);

    if ($(window).width() < 480) {
        background.css('max-height', 310)
        $('#top-banner').height(310);
    } else {
        background.css('max-height', 'none')
    }
}

function center() {
    var h = $('.full-img img').height();
    $('.full-img img').css('margin-top', -(h / 2) + 30)

    var w = $('.full-img img').width();
    $('.full-img img').css('margin-left', -(w / 2) - 15)
}

/* CONTENIDO DEL MODAL */

function modalText(name) {
    $('.modalBef').hide();
    console.log(name);
    $('.modalAf span').html(name);
    $('.modalAf').show();
    setTimeout(function() {
        $('.close-reveal-modal').trigger('click');
    }, 2000);
}

/* NAV MENU BELOW MID */

var menuState = 0,
    overlay =  $('#overlay');
    
$('.menu').on('click', function(e) {
    e.preventDefault();
    if (menuState == 0) {
        $('nav.top ul').fadeIn(100);
        menuState = 1;
        overlay.show();
    } else {
        $('nav.top ul').fadeOut(100);
        menuState = 0;
        overlay.hide();
    }
});

overlay.on('click', function(){
    $('.menu').trigger('click');
});


/* WALL */

function getMore(p){
    $.get(
    'http://www.reciclatumanzana.com.ar/php/get.php?page='+p,
    function(result) {
        var html = '';
        var i = 0;
        while (i < result.length) {
            html += '<li><img src="http://graph.facebook.com/' + result[i].facebook + '/picture?height=200&width=200"/></li>';
            i++;
        }
        $('#sumate').before(html);
    },
    'json');
}

getMore(0);

var page = 1;
var getState = 0;

$(window).on('scroll', function() {
    if ($(window).scrollTop() + $(window).height() == $(document).height() && getState === 0) {
        getState = 1;
        getMore(page);
        ++page;
        getState = 0;
    }
});

/* FIN WALL */

/* SCROLL */
function scrollToElement(selector, time, verticalOffset) {
    time = typeof(time) != 'undefined' ? time : 700;
    verticalOffset = typeof(verticalOffset) != 'undefined' ? verticalOffset : 0;
    element = $(selector);
    offset = element.offset();
    offsetTop = offset.top + verticalOffset-60;
    $('html, body').animate({
        scrollTop: offsetTop
    }, time);
}

$('.top .logo').on('click', function(e){
    e.preventDefault();
    scrollToElement('#unoT');
});

$('.top li a').on('click', function(e){
    e.preventDefault();
    var n = $(this).attr('data-id');
    if($(window).width() < 790 && menuState == 1){
        $('.menu').trigger('click');
    }
    scrollToElement('#'+n);
});

$('#top-banner').click(function(e) {
    e.preventDefault();
    scrollToElement('#iniciativa');
});
/* FIN SCROLL */

// SCROLL SPY
// Cache selectors
var lastId,
    topMenu = $(".top ul"),
    topMenuHeight = topMenu.outerHeight()+15,
    // All list items
    menuItems = topMenu.find("a"),
    // Anchors corresponding to menu items
    scrollItems = menuItems.map(function(){
      var item = $($(this).attr("href"));
      if (item.length) { return item; }
    });

// Bind to scroll
$(window).scroll(function(){
   // Get container scroll position
   var fromTop = $(this).scrollTop()+topMenuHeight;
   
   // Get id of current scroll item
   var cur = scrollItems.map(function(){
     if ($(this).offset().top < fromTop)
       return this;
   });
   // Get the id of the current element
   cur = cur[cur.length-1];
   var id = cur && cur.length ? cur[0].id : "";
   
   if (lastId !== id) {
       lastId = id;
       // Set/remove active class
       menuItems
         .parent().removeClass("active")
         .end().filter("[href=#"+id+"]").parent().addClass("active");
   }
});

